﻿using System;
using Microsoft.Win32;
using System.Data.Odbc;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;

namespace ExchangeFilesPharmWinService
{
    class WorkPlace
    {

        public string GetIP()
        {
            string ip = "";
            WebClient wclient = new WebClient();
            try
            {
                Stream stream = wclient.OpenRead("http://www.ip-ping.ru/");
                StreamReader sr = new StreamReader(stream);
                string newLine;
                Regex regex = new Regex("<div class=\"hc2\">(.*)</div>");
                while ((newLine = sr.ReadLine()) != null)
                {
                    Match match = regex.Match(newLine);
                    string str = match.Groups[1].ToString();
                    if (str != "") ip = str;
                }
                stream.Close();

            }
            catch (Exception)
            {
                ip = "";
            }

            if (ip == "")
                try
                {
                    Stream stream = wclient.OpenRead("http://2ip.ru/");
                    StreamReader sr = new StreamReader(stream);
                    string newLine;
                    Regex regex = new Regex("<big id=\"d_clip_button\">(.*)</big>");
                    while ((newLine = sr.ReadLine()) != null)
                    {
                        Match match = regex.Match(newLine);
                        string str = match.Groups[1].ToString();
                        if (str != "") ip = str;
                    }
                    stream.Close();

                }
                catch (Exception)
                {
                    ip = "";
                }

            if (ip == "")
                try
                {
                    Stream stream = wclient.OpenRead("http://yandex.ru/yandsearch?text=%D1%83%D0%B7%D0%BD%D0%B0%D1%82%D1%8C+%D1%81%D0%B2%D0%BE%D0%B9+ip&lr=47&promo=pumpkin");
                    StreamReader sr = new StreamReader(stream);
                    string newLine;
                    Regex regex = new Regex("<div class=\"z-fact__fact z-fact__fact_version_v4 z-fact__cell\">(.*)</div>");
                    while ((newLine = sr.ReadLine()) != null)
                    {
                        Match match = regex.Match(newLine);
                        string str = match.Groups[1].ToString();
                        if (str != "") ip = str;
                    }
                    stream.Close();

                }
                catch (Exception)
                {
                    ip = "";
                }

            return ip;
        }

        public int GetPharmCode()
        {
            string strConnection = @"DRIVER={InterSystems ODBC};SERVER=localhost;PORT=1972;DATABASE=USER;UID=_system;PWD=SYS;";
            using (OdbcConnection odbcCon = new OdbcConnection(strConnection))
            {
                try
                {
                    odbcCon.Open();
                    OdbcCommand odbcCmd = new OdbcCommand();
                    odbcCmd.CommandText = @"select extcode from sprsubdiv where destroyed = 0 and isout = 1";
                    odbcCmd.Connection = odbcCon;
                    return Convert.ToInt32(odbcCmd.ExecuteScalar());
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public string GetOSName(string productType)
        {
            System.OperatingSystem os = System.Environment.OSVersion;
            string osName = "Unknown";

            if (productType == "") productType = GetProductType();

            switch (os.Platform)
            {
                case System.PlatformID.Win32Windows:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            osName = "Windows 95";
                            break;
                        case 10:
                            osName = "Windows 98";
                            break;
                        case 90:
                            osName = "Windows ME";
                            break;
                    }
                    break;
                case System.PlatformID.Win32NT:
                    switch (os.Version.Major)
                    {
                        case 3:
                            osName = "Windws NT 3.51";
                            break;
                        case 4:
                            osName = "Windows NT 4";
                            break;
                        case 5:
                            if (os.Version.Minor == 0) osName = "Windows 2000";
                            else
                                if (os.Version.Minor == 1) osName = "Windows XP";
                                else
                                    if (os.Version.Minor == 2) osName = "Windows Server 2003";
                            break;
                        case 6:
                            if (productType == "work")
                            {
                                if (os.Version.Minor == 0) osName = "Windows Vista";
                                else
                                    if (os.Version.Minor == 1) osName = "Windows 7";
                                    else
                                        if (os.Version.Minor == 2) osName = "Windows 8";
                                        else
                                            if (os.Version.Minor == 3) osName = "Windows 8.1";
                            }
                            else
                                if (productType == "server")
                                {
                                    if (os.Version.Minor == 0) osName = "Windows Server 2008";
                                    else
                                        if (os.Version.Minor == 1) osName = "Windows Server 2008 R2";
                                        else
                                            if (os.Version.Minor == 2) osName = "Windows Server 2012";
                                            else
                                                if (os.Version.Minor == 3) osName = "Windows Server 2012 R2";
                                }
                            break;
                    }
                    break;
            }
            return osName + ", " + os.ServicePack.ToString() + ", " + os.Version.ToString();
        }

        public string GetProductType()
        {
            string value = String.Empty;
            string key = @"SYSTEM\CurrentControlSet\Control\ProductOptions";
            using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(key))
            {
                if (regKey != null)
                {
                    try
                    {
                        switch (regKey.GetValue("ProductType").ToString())
                        {
                            case "WinNT":
                                value = "work";
                                break;
                            case "LanmanNT":
                                value = "domen";
                                break;
                            case "ServerNT":
                                value = "server";
                                break;
                            default:
                                value = "other";
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                    return value;
                }
                else
                    return "не удалось найти указанный ключ";
            }
        }

        //private static string GetVersionFromRegistry()
        //{
        //    string retvers = "";
        //    try
        //    {
        //        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
        //            RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        //        {
        //            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
        //            {
        //                if (versionKeyName.StartsWith("v"))
        //                {

        //                    RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
        //                    string name = (string)versionKey.GetValue("Version", "");
        //                    string sp = versionKey.GetValue("SP", "").ToString();
        //                    string install = versionKey.GetValue("Install", "").ToString();
        //                    if (install == "") //no install info, must be later.
        //                        retvers += versionKeyName + "  " + name + "\n";
        //                    else
        //                    {
        //                        if (sp != "" && install == "1")
        //                        {
        //                            retvers += versionKeyName + "  " + name + "  SP" + sp + "\n";
        //                        }

        //                    }
        //                    if (name != "")
        //                    {
        //                        continue;
        //                    }
        //                    foreach (string subKeyName in versionKey.GetSubKeyNames())
        //                    {
        //                        RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
        //                        name = (string)subKey.GetValue("Version", "");
        //                        if (name != "")
        //                            sp = subKey.GetValue("SP", "").ToString();
        //                        install = subKey.GetValue("Install", "").ToString();
        //                        if (install == "") //no install info, must be later.
        //                            retvers += versionKeyName + "  " + name + "\n";
        //                        else
        //                        {
        //                            if (sp != "" && install == "1")
        //                            {
        //                                retvers += "  " + subKeyName + "  " + name + "  SP" + sp + "\n";
        //                            }
        //                            else if (install == "1")
        //                            {
        //                                retvers += "  " + subKeyName + "  " + name + "\n";
        //                            }

        //                        }

        //                    }

        //                }
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //    return retvers;

        //}

        //private static void Get45or451FromRegistry()
        //{
        //    using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
        //       RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"))
        //    {
        //        int releaseKey = (int)ndpKey.GetValue("Release");
        //        {
        //            if (releaseKey == 378389)

        //                Console.WriteLine("The .NET Framework version 4.5 is installed");

        //            if (releaseKey == 378758)

        //                Console.WriteLine("The .NET Framework version 4.5.1  is installed");

        //        }
        //    }
        //}
    }
}
